const getLocation = () => {
  return {};
};
let listeners: any[] = [];
let location = getLocation();

const push = (to: string, state: any) => {
  location = to;
  window.history.pushState(to, '', state);
  listeners.forEach((fn) => fn());
};
const listen = (fn: any) => {
  listeners.push(fn);
  return () => {
    listeners = listeners.filter((func) => func !== fn);
  };
};
window.addEventListener('popstate', () => {
  location = getLocation();
  listeners.forEach((fn) => fn());
});

export default {
  get location() {
    return location;
  },
  push,
  listen,
};
